package com.example.tellwin.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;

import androidx.annotation.Nullable;

import com.apkfuns.logutils.LogUtils;
import com.example.tellwin.R;
import com.example.tellwin.mine.bean.PayResult;
import com.example.tellwin.view.LoadingDialog;


/**
 * Created by Administrator on 2016/12/2
 */
public abstract class CpBaseFragment extends BaseFragment implements BaseContract.BaseView {
    protected Handler handler = new Handler();
    private LoadingDialog loadingDialog;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Activity activity = getActivity();
        if (activity instanceof BaseActivity) {
            BaseActivity baseActivity = (BaseActivity) activity;
            loadingDialog = baseActivity.loadingDialog;
        }
        if (loadingDialog == null) {
            loadingDialog = new LoadingDialog(getActivity());
        }
        return super.onCreateView(inflater, container, savedInstanceState);
    }


    @Override
    public void onLoading() {
        showDialog();
    }

    @Override
    public void complete() {
        dismissDialog();
    }

    @Override
    public void showError() {
        dismissDialog();
        toast(getResources().getString(R.string.system_error));
    }

    @Override
    public void showToast(String msg) {
        toast(msg);
    }

    public void hideSoftKeyboard() {
        View view = getActivity().getWindow().peekDecorView();
        if (view != null) {
            InputMethodManager inputmanger = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
            inputmanger.hideSoftInputFromWindow(view.getWindowToken(), 0);

        }
    }

    public void showSoftKeyboard() {
        View view = getActivity().getWindow().peekDecorView();
        if (view != null) {
            InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);

        }
    }


    public void showDialog() {
        LogUtils.i("showDialog");
        loadingDialog.show();
    }

    public void dismissDialog() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
            LogUtils.i("dismissDialog");
        }
    }

    public void startActivity(Class<?> cl) {
        Intent intent = new Intent(getContext(), cl);
        startActivity(intent);
    }

    public void startActivity(Bundle bundle, Class<?> cl) {
        Intent intent = new Intent(getContext(), cl);
        intent.putExtras(bundle);
        startActivity(intent);
    }

}
